package com.zuimei.zuimei.utils;


import android.graphics.Bitmap;

import com.squareup.picasso.Transformation;

public class ImgUtil {

    public static Transformation getTransformation(final int targetWidth) {
        Transformation transformation = new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                Bitmap result = null;
                int targetHeight;
                try {
                    double aspecRatio = (double) source.getHeight() / (double) source.getWidth();
                    targetHeight = (int) (targetWidth * aspecRatio);//目标高度
                    result = Bitmap.createScaledBitmap(source, targetWidth,
                            targetHeight, false);
                    if (result != source) {
                        source.recycle();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
        return transformation;
    }

    public static Transformation getTransformation(final int targetWidth, final int targetHeight) {
        Transformation transformation = new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                Bitmap result = null;
                try {
                    result = Bitmap.createScaledBitmap(source, targetWidth,
                            targetHeight, false);
                    if (result != source) {
                        source.recycle();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
        return transformation;
    }
}
